<?php
/**
 * 账号
 * 
 * @author kaikaic
 * @date 2018-04-09
 *
 */

namespace service;

use think\Db;

class   AccountService {

	private static $table = "Account";

	public static function find($accountID) {
        $res = Db::name(self::$table)->where('id', $accountID)->find();
        $res['group'] =  Db::name("exam_group_member")->field('max(id) as gid,group_id')->where('account_id', $res['id'])->find();
		return $res;
	}

	public static function createUser($userName, $mobile,$avatarUrl,$openid) {
	    $info = Db::name(self::$table)->where('mobile', $mobile)->find();


        if ($info) {
            return $info['id'];
        } else {
            if(Db::name(self::$table)->where('openid', $openid)->find()){
                return false;
            }else{
                $passinfo = encrypt('111111','');
//            $userAddress = getLocation($mobile);
//            $address = $userAddress['data']['province'].$userAddress['data']['city'];
                $data = ['openid'=>$openid,'mobile' => $mobile,  'password' => $passinfo['pass'], 'salt' => $passinfo['salt'], 'create_time' => time(),'nick_name'=>$userName,'avatarUrl'=>$avatarUrl];
                //更新人数
                return Db::name(self::$table)->insertGetId($data);
            }

        }
    }

    public static function save($data, $id) {
	    unset($data['id']);
        return Db::name(self::$table)->where('id', $id)->update($data);
    }

    public static function delete($id) {
        return Db::name(self::$table)->where('id', $id)->delete();
    }

    public static function dologin($mobile) {
         $res = Db::name(self::$table)->where("mobile",$mobile)->find();
            $res['group'] =  Db::name("exam_group_member")->field('max(id) as gid')->where('account_id', $res['id'])->find();
            return $res;
    }

    public static function dofield($id) {
        return Db::name(self::$table)->field("id,cover,real_name,nick_name,mobile,status,create_time")->where("id",$id)->find();
    }

    public static function doreg($mobile) {
        return Db::name(self::$table)->where("mobile",$mobile)->count();
    }

    public static function checkPhone($data) {
        $num =  Db::name(self::$table)->where("mobile",$data['mobile'])->where("id",$data['id'])->find();
        if($num){
            if(isset($data['password'])){
                $passArr = encrypt($data['password'], $num['salt']);
                $data['password'] = $passArr['pass'];
            }
            self::save($data,$data['id']);
            return self::find($data['id']);
        }else{
            $num2 = Db::name(self::$table)->where("mobile",$data['mobile'])->find();
            if($num2){
                return false;
            }else{
                if(isset($data['password'])){
                    $passArr = encrypt($data['password'], $num['salt']);
                    $data['password'] = $passArr['pass'];
                }
                self::save($data,$data['id']);
                return self::find($data['id']);
            }
        }
    }

    public static function getcancel(){
        return Db::name("order_cancel")->order("sort asc")->select();
    }
}
